package org.anachronos.jeep.ui.trace;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.emf.common.ui.URIEditorInput;
import org.eclipse.emf.common.util.URI;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.handlers.HandlerUtil;

public class OpenTraceCommand extends AbstractHandler {

	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {
		final Shell shell = HandlerUtil.getActiveShell(event);
		final FileDialog fileDialog = new FileDialog(shell);
//		fileDialog.setFilterExtensions(new String[] { "*.trace" });
		final String traceFile = fileDialog.open();
		if (traceFile != null) {
			final IWorkbenchWindow workbenchWindow = HandlerUtil.getActiveWorkbenchWindow(event);
			final URIEditorInput editorInput = new URIEditorInput(URI.createFileURI(traceFile));
			try {
				workbenchWindow.getActivePage()
					.openEditor(editorInput, "org.anachronos.jeep.ui.editor.trace");
			} catch (final PartInitException e) {
				throw new ExecutionException("", e);
			}
		}
		return null;
	}

}
